package com.rpm.rpm.file;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

/**
 * 科研项目文件的下载
 * @author Hongduhao
 * @date : 2024/3/1
 */
public class FileDownload {
    public static void main(String[] args) {
        String fileURL = "/ttt"; // 实际的文件下载地址
        String saveDir = "/ttt"; // 文件保存目录

        try {
            URL url = new URL(fileURL);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            connection.connect();

            InputStream is = connection.getInputStream();
            FileOutputStream fos = new FileOutputStream(saveDir + File.separator + "downloaded_file_name"); // 替换为实际的文件名

            byte[] buffer = new byte[4096];
            int bytesRead;
            while ((bytesRead = is.read(buffer)) != -1) {
                fos.write(buffer, 0, bytesRead);
            }

            fos.close();
            is.close();

            System.out.println("File downloaded successfully");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
